package com.hy.heath.controller;

import com.hy.heath.common.R;
import com.hy.heath.pojo.entity.HealthKnowledge;
import com.hy.heath.pojo.entity.User;
import com.hy.heath.service.HealthKnowledgeService;
import com.hy.heath.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HealthKnowledgeService healthKnowledgeService;

    @PostMapping("/login")
    public R<String> login(@RequestBody User user){
        Boolean  isSuccess = userService.login(user);
        if (!isSuccess) {
            return R.error("登录失败，请检查用户名和密码");
        }
        return R.success("登录成功");
    }

    @GetMapping("/health-knowledge")
    public List<HealthKnowledge> getAllHealthKnowledge() {
        return healthKnowledgeService.getAllHealthKnowledge();
    }

    @PostMapping("/register")
    public R<String> register(@RequestBody User user){
        Boolean  isSuccess = userService.register(user);
        if (!isSuccess) {
            return R.error("注册失败，请检查输入信息");
        }
        return R.success("注册成功");
    }
}
